package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.ModuleDto;
import com.itheima.admin.pojo.Module;
import com.itheima.admin.service.IModuleService;
import com.itheima.admin.vo.ModuleListVo;
import com.itheima.admin.vo.ModuleVo;
import com.itheima.admin.vo.QModuleVo;
import com.itheima.admin.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private IModuleService moduleService;

    @PostMapping("/list")
    @ApiOperation("分页查询模块")
    public PageVo<ModuleVo> queryByPage(@RequestBody ModuleDto moduleDto){

        if (moduleDto.getCurrentPage() == null){
            moduleDto.setCurrentPage(1);
        }
        if (moduleDto.getPageSize() == null){
            moduleDto.setPageSize(10);
        }
        return moduleService.queryByPage(moduleDto);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询模块")
    public QModuleVo queryById(@PathVariable("id") String moduleId){
        return moduleService.queryById(moduleId);
    }

    @PostMapping("/add")
    @ApiOperation("新增模块")
    public Result addModule(@RequestBody QModuleVo qModuleVo){

        boolean result = moduleService.addModule(qModuleVo);

        return new Result(result,null,result?"添加模块成功":"添加模块失败");

    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除模块")
    public Result deleteModule(@PathVariable("id") String moduleId){

        boolean result = moduleService.deleteModule(moduleId);

        return new Result(result,null,result?"删除模块成功":"删除模块失败");
    }

    @PutMapping("/edit")
    @ApiOperation("修改模块信息")
    public Result editModule(QModuleVo qModuleVo){
        boolean result = moduleService.editModule(qModuleVo);

        return new Result(result,null,result?"模块修改成功":"模块修改失败");
    }

    @GetMapping("/listall")
    @ApiOperation("查询所有模块")
    public List<ModuleListVo> queryAllModules(){
        return moduleService.queryAllModules();
    }

}
